<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\member\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\member\model\MemberStatus as MemberStatusModel;
use app\user\model\Member as MemberModel;
use think\Db;

/**             
 * 行为管理控制器
 * @package app\admin\controller
 */
class Memberlevel extends Admin
{

    public function index()
    {
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $show=MemberStatusModel::isShow();
        $level_price=Db::name('goods_pricelevel')->where('status=1')->column("priceid,pricename");
        $map = $this->getMap();
        // 数据列表
        $data_list = Db::name('member_level')->where($map)->order('sort asc,id desc')->paginate();

        // 分页数据
        $page = $data_list->render();

        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('会员等级')// 设置页面标题
            ->setTablename('member_level')// 设置数据表名
            ->setSearch(['name' => '会员等级'])// 设置搜索参数
            ->setPrimaryKey('id')
            ->addColumns([ // 批量添加列
                ['id', '等级编号'],
                ['name', '会员等级'],
                ['level_price', '价格等级','','',$level_price],
                ['code', 'CODE'],
                ['status', '等级状态','switch'],
                ['is_show', '是否展示','status','',$show],
                ['right_button', '操作', 'btn']
            ])
            ->addFilter('level_price',$level_price)
            ->addTopButtons('add,delete')// 批量添加顶部按钮
            ->addRightButtons('edit')// 批量添加右侧按钮
            ->addRightButton('delete',['icon' => 'fa fa-times', 'title' => '删除', 'href' => url('delete_level', ['id' => '__id__'])])
            ->setRowList($data_list)// 设置表格数据
            ->setPages($page)// 设置分页数据
            ->fetch(); // 渲染页面
    }

    //新增会员等级
    public function add()
    {
        $price_level=Db::name('goods_pricelevel')->where('status=1')->column("priceid,pricename");
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Memberlevel');
            // 验证失败 输出错误信息
            if (true !== $result) {
                return $this->error($result);
            }
            if(is_numeric($data['sort'])==false){
                return $this->error('请输入一个数字排序');
            }
            $data['createtime']=time();
            $res=Db::name('member_level')->insertGetid($data);
            if ($res) {
                $details = "所属模块('member'),操作日志('新增会员等级'),等级id('$res')";
                action_log('memberlevel_add', 'member', $res, UID, $details);
                return $this->success('新增成功', cookie('__forward__'));
            } else {
                return $this->error('新增失败');
            }
        }
            // 使用ZBuilder快速创建表单
            return ZBuilder::make('form')
                ->setPageTitle('新增会员等级')// 设置页面标题
                ->addFormItems([ // 批量添加表单项
                    ['text', 'name', '会员等级', '必填，可由英文字母、数字组成'],
                    ['select', 'level_price', '价格等级','',$price_level],
                    ['text', 'sort', '排序数字'],
                    ['text', 'code', 'CODE'],
                    ['radio', 'status', '状态', '', ['禁用', '启用'], 1]
                ])
                ->layout(['name'=>3,'level_price'=>3,'code'=>3,'sort'=>3])
                ->fetch();
    }
    //编辑会员等级
    public function edit($id = null){
        $price_level=Db::name('goods_pricelevel')->where('status=1')->column("priceid,pricename");
        $show=MemberStatusModel::isShow();
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Memberlevel');
            // 验证失败 输出错误信息
            if (true !== $result) {
                return $this->error($result);
            }
            if(is_numeric($data['sort'])==false){
                return $this->error('请输入一个数字排序');
            }
            $res=Db::name('member_level')->update($data);
            if ($res) {
                $details = "所属模块('member'),操作日志('编辑会员等级'),等级id('$id')";
                action_log('memberlevel_edit', 'member', $id, UID, $details);
                return $this->success('新增成功', cookie('__forward__'));
            } else {
                return $this->error('新增失败');
            }
        }
        $info = Db::name('member_level')->where('id',$id)->find();
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('会员等级编辑')// 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['hidden','id'],
                ['text', 'name', '会员等级', '必填，可由英文字母、数字组成'],
                ['select', 'level_price', '价格等级','',$price_level],
                ['text', 'sort', '排序数字'],
                ['text', 'code', 'CODE'],
                ['radio', 'is_show', '是否展示', '',$show],
                ['radio', 'status', '状态', '', ['禁用', '启用'], 1]
            ])
            ->setFormData($info) // 设置表单数据
            ->layout(['name'=>3,'level_price'=>3,'code'=>3,'sort'=>3])
            ->fetch();
    }
    //单个删除会员等级
    public function delete_level(){
        $id = $this->request->param('id');
        $res=Db::name("member_level")->where('id',$id)->delete();
        if ($res) {
            $details = "所属模块('member'),操作日志('删除会员等级'),等级id('$id')";
            action_log('memberlevel_delete', 'member', $id, UID, $details);
            return $this->success('删除成功',cookie('__forward__'));
        } else {
            return $this->error('删除失败');
        }
    }
    //批量删除会员等级
    public function delete($record = [])
    {
        $ids        = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        $id=implode(',',$ids);
        foreach ($ids as $values){
            $res=Db::name("member_level")->where('id',$values)->delete();
        }
        if ($res) {
            $details = "所属模块('member'),操作日志('批量删除会员等级'),等级id('$id')";
            action_log('memberlevel_deletes', 'member', $id, UID, $details);
            return $this->success('删除成功',cookie('__forward__'));
        } else {
            return $this->error('删除失败');
        }
    }
}